
//
//  BDCommondTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/3/16.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDCommondTableViewCell.h"
#import "YYStarView.h"
#import "BDShopCommonModel.h"

@interface BDCommondTableViewCell ()

@property (nonatomic, strong) UIImageView *avatarImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) YYStarView *starView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *bottomLabel;

@end

@implementation BDCommondTableViewCell

+ (CGFloat)getCellHeightWithModel:(id)model{
    CGFloat bottomlabelWidth = kBDScreenWidth - (10 +42 +6) - 10;
    NSString *str = ((BDShopCommonModel *)model).comment;
    return 10+5+16+6+12+5+10+[str heightWithFont:[UIFont systemFontOfSize:13] width:bottomlabelWidth] + 15;
}

- (void)configeWithModel:(id)model{
    BDShopCommonModel *commonModel = model;
    [self.avatarImage sd_setImageWithURL:[NSURL URLWithString:commonModel.cover] placeholderImage:kBDUserAvatar];
    self.nameLabel.text = commonModel.nickname;
    NSDate *curDate = [NSDate dateWithTimeIntervalSince1970:commonModel.created_at.floatValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateStr = [formatter stringFromDate:curDate];
    self.timeLabel.text = dateStr;
    self.bottomLabel.text = commonModel.comment;
    [self.starView displayRating:commonModel.score];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.avatarImage.size = CGSizeMake(42, 42);
    self.avatarImage.left = 10;
    self.avatarImage.top = 10;
    self.avatarImage.layer.masksToBounds = YES;
    self.avatarImage.layer.cornerRadius = self.avatarImage.width / 2.0;
    
    self.nameLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.size = CGSizeMake([self.nameLabel.text widthWithFont:self.nameLabel.font], self.nameLabel.font.pointSize+1);
    self.nameLabel.top = self.avatarImage.top + 5;
    self.nameLabel.left = self.avatarImage.right + 6;
    
    [self.starView setStarWidth:12 starSpace:1.2 andDelegate:nil];
    self.starView.left = self.nameLabel.left;
    self.starView.top = self.nameLabel.bottom + 6;
    
    self.timeLabel.font = [UIFont systemFontOfSize:13];
    self.timeLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    self.timeLabel.size = CGSizeMake([self.timeLabel.text widthWithFont:self.timeLabel.font], self.timeLabel.font.pointSize+1);
    self.timeLabel.left = self.starView.right + 6;
//    self.timeLabel.left = self.nameLabel.left;
    self.timeLabel.top = self.nameLabel.bottom + 5;
    
    self.bottomLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    self.bottomLabel.font = [UIFont systemFontOfSize:14];
    self.bottomLabel.numberOfLines = 0;
    self.bottomLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGFloat bottomlabelWidth = self.width - self.nameLabel.left - 10;
    self.bottomLabel.size = CGSizeMake(bottomlabelWidth, [self.bottomLabel.text heightWithFont:self.bottomLabel.font width:bottomlabelWidth]);
    self.bottomLabel.left = self.nameLabel.left;
    self.bottomLabel.top = self.timeLabel.bottom + 7;

}

- (UIImageView *)avatarImage{
    if (!_avatarImage) {
        self.avatarImage = [[UIImageView alloc] init];
        [self.contentView addSubview:_avatarImage];
    }
    return _avatarImage;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        self.nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

-(YYStarView *)starView{
    if (!_starView) {
        self.starView = [[YYStarView alloc] init];
        [self addSubview:_starView];
    }
    return _starView;
}
-(UILabel *)timeLabel{
    if (!_timeLabel) {
        self.timeLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}
-(UILabel *)bottomLabel{
    if (!_bottomLabel) {
        self.bottomLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_bottomLabel];
    }
    return _bottomLabel;
}

@end
